<template>
    <section>
        <!--工具条-->
        <div class="section-content clearfix">
            <el-col :span="24" class="" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filters">
                    <el-form-item>
                        <el-input v-model="filters.name" placeholder="设备名字" @keyup.enter.native="getUsers()"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-input v-model="filters.deviceSn" placeholder="网关SN" @keyup.enter.native="getUsers()">
                        </el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-select v-model="filters.formula"  placeholder="设备模板" @change="getUsers()">
                            <el-option label="全部" value=" "></el-option>
                            <el-option v-for="item in formulalist" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item class="">
                        <el-button :type="buttonColor" v-on:click="getUsers">查询</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button :type="buttonColor" :disabled="sels.length===0||filters.formula==''" v-on:click="farUpgradeEdit(1,'','远程设备下载')">下载固件至本地</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button :type="buttonColor" :disabled="sels.length===0||filters.formula==''" v-on:click="farUpgradeEdit(1,'','远程设备升级')">本地固件升级</el-button>
                    </el-form-item>
                    <el-form-item >
                        <el-button :type="buttonColor" :disabled="sels.length===0" v-on:click="getFarMessage">获取固件信息</el-button>
                    </el-form-item>
                    <!-- <el-form-item v-if="menubt.indexOf('/device_upgrade/upgrade_by_condition')>-1">
                            <el-button :type="buttonColor" v-on:click="upgradeEdit(1)">一键升级设备</el-button>
                        </el-form-item> -->
                    <!-- <el-form-item>
                            <el-button :type="buttonColor" v-on:click="upgradeEdit(2)">一键升级设备</el-button>
                        </el-form-item> -->
                </el-form>
            </el-col>

            <!--列表-->
            <el-table :data="query" highlight-current-row v-loading="listLoading" @selection-change="selsChange" :header-row-style="{backgroundColor: titleColor}"
                style="width: 100%;">
                <el-table-column type="selection">
                </el-table-column>
                <el-table-column align="center" type="index" label="序号"  width="80":index="indexMethod">
                </el-table-column>
                <el-table-column align="center" prop="deviceName" label="设备名字">
                </el-table-column>
                <el-table-column align="center" prop="state" label="状态" width="80" :formatter="formatstate">

                </el-table-column>
                <el-table-column align="center" prop="deviceSn" label="网关SN">
                </el-table-column>
                <!-- <el-table-column prop="softCode" label="固件编码" width="80">
                </el-table-column>
                <el-table-column prop="hardwareVersion" label="硬件版本" width="80">
                </el-table-column>
                <el-table-column prop="softVersion" label="软件版本" width="80">
                </el-table-column> -->
                <!-- <el-table-column v-if="menubt.indexOf('/device_upgrade/batch_upgrade')>-1"  label="设备升级状态"  >
                                
                        <template scope="scope">
                            <span style="color:red">{{upgradeConnState(scope.row)}}</span>
                        </template>
                    </el-table-column> -->

                <el-table-column align="center" label="设备升级状态">
                    <template scope="scope">
                        <!-- <span v-if="scope.row.upgradeLowerState!=-1" style="color:red">{{upgradeConnState(scope.row)}}</span> -->
                        <span v-if="scope.row.upgradeLowerState!=-1" style="color:red">{{upgradeLowerState(scope.row)}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="进度" align="center">

                    <template scope="scope">
                        <el-progress v-if="scope.row.upgradeLowerState!=-1" :text-inside="true" :stroke-width="18" style="color: greenyellow"
                            :percentage="scope.row.upgradeLowerProgress"></el-progress>
                    </template>
                </el-table-column>

                <!-- <el-table-column prop="lastUpgradeConnTime" label="设备升级时间">
                    </el-table-column> -->
                <el-table-column align="center" prop="lastUpgradeLowerTime" label="设备升级时间">
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template scope="scope">

                        <!-- <span  v-if="menubt.indexOf('/device_upgrade/batch_upgrade')>-1">
    
                                <el-button @click="handleEdit( scope.row,1)" type="small" plain>设备升级</el-button>
                            </span> -->
                        <span v-if="menubt.indexOf('/device_lower_upgrade/batch_upgrade')>-1" style="display:flex;align-items:center;justify-content: center;">

                            <!-- <el-button @click="handleEdit( scope.row,2)" type="small" plain>设备升级</el-button> -->
                            <i title="设备升级" type="warning" @click="handleEdit(scope.row,2)" class="pb-icon icon-shengji"></i>
                            <i title="下载固件至本地" type="warning" @click="farUpgradeEdit(1,scope.row,'远程设备下载')" class="pb-icon icon-xiazaigujian" ></i>
                            <i title="本地固件升级" type="warning" @click="farUpgradeEdit(1,scope.row,'远程设备升级')" class="pb-icon  icon-gujianshengji" ></i>
                            <!-- <i title="设备升级" type="warning" @click="special(scope.$index, scope.row)" class="pb-icon icon-Photoshop"></i> -->
                        </span>
                    </template>

                </el-table-column>
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <!-- <el-button v-if="menubt.indexOf('/device_upgrade/batch_upgrade')>-1" type="danger" @click="batchRemove(1)" :disabled="this.sels.length===0">批量升级</el-button> -->
                <el-button v-if="menubt.indexOf('/device_lower_upgrade/batch_upgrade')>-1" type="danger"
                    @click="batchRemove(2)" :disabled="this.sels.length===0">批量设备升级</el-button>
                    <el-pagination layout="sizes,prev, pager, next" @current-change="handleCurrentChange"
                    @size-change="handleSizeChange" :page-size="10" :total="total" :page-sizes="[10, 20, 50, 100]"
                    style="float:right;">
                    </el-pagination>
            </el-col>
        </div>
        <!--编辑界面-->
        <el-dialog title="命令" v-show="editFormVisible" v-model="editFormVisible" :custom-class="dialogTitleColor" @close="editFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
                    <el-form-item v-show="softCode" label="软件编码">
                        <span>{{softCode}}</span>
                    </el-form-item>
                    <el-form-item v-show="softVersion" label="软件版本">
                        <span>{{softVersion}}</span>
                    </el-form-item>
                    <el-form-item v-show="paramsCode" label="参数编码">
                        <span>{{paramsCode}}</span>
                    </el-form-item>
                    <el-form-item v-show="paramsVersion" label="参数版本">
                        <span>{{paramsVersion}}</span>
                    </el-form-item>
                    <el-form-item label="是否单独导参数">
                        <el-radio-group v-model="parameters" @change="parametersget()">
                            <el-radio label="0">否</el-radio>
                            <el-radio label="1">是</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="设备固件包">
                        <el-select v-model="editForm.deviceUpgradeId" placeholder="请选择" :disabled="parameters==1">
                            <el-option v-for="(item,index) in bag" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="设备固件参数表">
                        <el-select v-model="editForm.deviceArgumentId" placeholder="请选择">
                            <el-option v-for="(item,index) in parameter" :key="item.id" :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="地址号" v-if="lowerType==1">
                        <!-- <el-input v-model="editForm.slaveSN" type="input"></el-input> -->
                        <el-select v-model="editForm.slaveSN" placeholder="请选择" @change="showPara">
                            <el-option v-for="(item,index) in sigleslavelist" :key="item.id" :label="item.name" :value="item.slaveSn"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:center;">
                    <el-button @click.native="editFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="editSubmit" :loading="editLoading">提交</el-button>
                </div>
         

            </div>
        </el-dialog>
        <el-dialog title="命令" v-show="upgradeVisible" v-model="editFormVisible" :custom-class="dialogTitleColor" @close="upgradeVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="是否单独导参数">
                        <el-radio-group v-model="parameters" @change="parametersget()">
                            <el-radio label="0">否</el-radio>
                            <el-radio label="1">是</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="设备固件包">
                        <el-select v-model="editForm.deviceUpgradeId" placeholder="请选择" :disabled="parameters==1">
                            <el-option v-for="(item,index) in bag" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="设备固件参数表">
                        <el-select v-model="editForm.deviceArgumentId" placeholder="请选择">
                            <el-option v-for="(item,index) in parameter" :key="item.id" :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <!-- <el-form-item label="ftpPort">
                            <el-input v-model="editForm.ftpPort" type="input"></el-input>
                        </el-form-item>
                        <el-form-item label="ftp账号">
                            <el-input v-model="editForm.ftpUser" type="input"></el-input>
                        </el-form-item>
    
                        <el-form-item label="ftp密码">
                            <el-input type="input" v-model="editForm.ftpPassword "></el-input>
                        </el-form-item> -->
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:center;">
                    <el-button @click.native="upgradeVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="upgrade" :loading="editLoading">提交</el-button>
                </div>
  

            </div>
        </el-dialog>
        <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" v-model="addFormVisible" :custom-class="dialogTitleColor" @close="addFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
                    <el-form-item label="名字" prop="name">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input type="textarea" v-model="addForm.remark"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>

        <!-- 单个 -->
        <el-dialog title="下载固件至本地" v-show="sigledownloadVisible" v-model="sigledownloadVisible" :custom-class="dialogTitleColor" @close="sigledownloadVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="firmwareForm" label-width="120px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="固件包">
                        <el-select v-model="firmwareForm.deviceUpgradeId" placeholder="请选择" >
                            <el-option v-for="(item,index) in bagslave" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <!-- <el-form-item label="从机号">
                        <el-select v-model="firmwareForm.slaveSn" placeholder="请选择">
                            <el-option v-for="(item,index) in sigleslavelist" :key="item.id" :label="item.name"
                                :value="item.slaveSn">
                            </el-option>
                        </el-select>
                    </el-form-item> -->
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:center;">
                    <el-button @click.native="sigledownloadVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="download" :loading="editLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 单个 -->
        <el-dialog title="本地固件升级" v-show="siglefarUpgradeVisible" v-model="siglefarUpgradeVisible" :custom-class="dialogTitleColor" @close="siglefarUpgradeVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="firmwareForm" label-width="120px" :rules="editFormRules" ref="editForm">
                    <!-- <el-form-item label="从机号">
                        <el-select v-model="firmwareForm.slaveSn" placeholder="请选择" @change="selectSlave()">
                            <el-option v-for="(item,index) in sigleslavelist" :key="item.id" :label="item.name"
                                :value="item.slaveSn">
                            </el-option>
                        </el-select>
                    </el-form-item> -->
                    <el-form-item v-show="dlSoftCode" label="下载固件编码">
                        <span>{{dlSoftCode}}</span>
                    </el-form-item>
                    <el-form-item  v-show="dlSoftVersion" label="固件版本">
                        <span>{{dlSoftVersion}}</span>
                    </el-form-item>
                    <el-form-item v-show="!dlSoftCode || !dlSoftVersion" label="提示：">
                        <span>当前设备没有下载固件</span>
                    </el-form-item>
                    <el-form-item v-show="dlSoftVersion || dlSoftCode" label="固件包">
                        <el-select v-model="firmwareForm.deviceUpgradeId" placeholder="请选择" >
                            <el-option v-for="(item,index) in bagslave" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:center;">
                    <el-button @click.native="siglefarUpgradeVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="farUpgrade" :loading="editLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    // import util from './../common/js/util'
    //import NProgress from 'nprogress'
    import {
        device_model_query,
        device_model_post,
        device_model_put,
        device_model_delete,
        device_query,
        device_cmd,
        device_attr_query,
        device_upgrade_query,
        device_argument_query,
        device_upgrade_batch_upgrade,
        device_upgrade_upgrade_by_condition,
        device_slave_list,
        device_batch_download,
        device_batch_upgrade,
        device_template_query
    } from '../../../api/api';

    export default {
        data() {
            return {
                pageSize:10,
                t: '',
                lowerType: '',
                parameters: '0',
                upgradeVisible: false,
                sigledownloadVisible:false,
                siglefarUpgradeVisible:false,
                bagid: '',
                parameterid: '',
                bag: [],
                parameter: [],
                filters: {
                    name: '',
                    deviceSn: '',
                    formula:''
                },
                query: [],
                total: 0,
                page: 1,
                listLoading: false,
                sels: [], //列表选中列

                editFormVisible: false, //编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }]
                },
                //编辑界面数据
                editForm: {
                    ftpUser: '',
                    ftpPort: '',
                    deviceUpgradeId: '',
                    deviceArgumentId: '',
                    ftpPassword: '',
                    slaveSN: ''
                },
                firmwareForm:{
                    deviceUpgradeId:'',
                    slaveSn:''
                },
                dlSoftCode:'',
                dlSoftVersion:'',
                paramsCode:'',//参数编码
                paramsVersion:'',//参数版本
                softCode:'',//软件编码
                softVersion:'',//软件版本
                sigleslavelist:[],
                bagslave:[],
                addFormVisible: false, //新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
                    path: [{
                            required: true,
                            message: '请输入path',
                            trigger: 'blur'
                        }

                    ]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    remark: ''
                },
                parametype: '',
                formulalist:[]
            }
        },
        computed:{
            buttonColor(){
                return this.$store.state.buttonColor
            },
            titleColor(){
                return this.$store.state.titleColor
            },
            dialogTitleColor(){
                return this.$store.state.dialogTitleColor
            },
        },
        methods: {
            showPara(val){
                console.log(this.sigleslavelist)
                let obj = {}
                obj = this.sigleslavelist.find(item =>{
                    return item.slaveSn == val
                })
                this.softCode = obj.softCode
                this.softVersion = obj.softVersion
                this.paramsCode = obj.paramsCode
                this.paramsVersion = obj.paramsVersion
            },//设备升级的参数显示，随着从机变化而改变
            indexMethod(index) {
                console.log(this.page)
                let num = (this.page - 1) * this.pageSize + index + 1;
                return num;
            },
            handleSizeChange(val) {
                this.pageSize = val;

                this.getUsers();
            },
            parametersget() {

                if (this.parameters == 1) {
                    this.editForm.deviceUpgradeId = '';
                    if (this.editForm.type == 1) {
                        this.editForm.type = 3
                    } else {
                        this.editForm.type = 4
                    }
                } else {
                    this.editForm.type = this.parametype;
                }
            },
            upgrade() {
                
                if (!this.editForm.deviceArgumentId && !this.editForm.deviceUpgradeId) {
                    this.$message({
                        message: '请选择固件包或参数表',
                        type: 'warning'
                    });
                    return false;
                }
                if ( !this.editForm.deviceArgumentId) {
                    this.$message({
                        message: '请选择参数表',
                        type: 'warning'
                    });
                    return false;
                }
                this.$confirm('确认升级吗？', '提示', {}).then(() => {
                    // this.editLoading = true;
                    //NProgress.start();
                    let para = Object.assign({}, this.editForm);

                    device_upgrade_upgrade_by_condition(para).then((res) => {
                        aler(res.code)
                        this.begCode(res.code, res.message);

                        if (res.code == 0) {
                            this.editLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '提交成功',
                                type: 'success'
                            });
                            this.$refs['editForm'].resetFields();
                            this.upgradeVisible = false;
                            this.getUsers();
                        }
                    });
                });
            },
            upgradeEdit: function (type) {


                this.parameters = '0';
                this.parametype = type;
                this.getlist(type);

                this.upgradeVisible = true;
                this.editForm = {
                    type: type,
                    ftpUser: '',
                    ftpPort: '',
                    deviceUpgradeId: '',
                    deviceArgumentId: '',
                    ftpPassword: ''
                }
            },
            upgradeConnState(row, column) {
                return row.upgradeConnState ==-1 ? ' ' : row.upgradeConnState == 0 ? '不需要更新固件' : row.upgradeConnState == 1 ? '数据解析错误' : row
                    .upgradeConnState ==
                    2 ? '读取内存失败' : row.upgradeConnState == 3 ? '不需要下载' :
                    row.upgradeConnState == 4 ? '连接服务器失败' : row.upgradeConnState == 5 ? '连接服务器成功' : row
                    .upgradeConnState ==
                    6 ? '网关空间不足' : row.upgradeConnState == 7 ? '内存空间不足' :
                    row.upgradeConnState == 8 ? '开始下载' : row.upgradeConnState == 9 ? '下载成功' : row
                    .upgradeConnState ==
                    10 ? '下载失败' : row.upgradeConnState == 11 ?
                    '数据校验正确' : row.upgradeConnState == 12 ? '数据校验错误' : row.upgradeConnState == 13 ?
                    '内存擦除失败' : row.upgradeConnState == 14 ? '开始写入内存' :
                    row.upgradeConnState == 15 ? '写入内存成功' : row.upgradeConnState == 16 ?
                    '写入内存失败' :
                    row.upgradeConnState == 17 ? '开始更新固件' : row.upgradeConnState == 18 ? '更新固件成功' :
                    row.upgradeConnState == 19 ? '更新固件失败' : row.upgradeConnState == 20 ? '更新配置成功' : row
                    .upgradeConnState ==
                    21 ? '更新配置失败' : row.upgradeConnState == 22 ? '正在更新固件' : row.upgradeConnState == 23 ?
                    '正在更新配置' : row.upgradeConnState == 100 ?
                    '数据解析错误' : row.upgradeConnState == 101 ?
                    '连接服务器失败' : row.upgradeConnState == 102 ?
                    '连接服务器成功' : row.upgradeConnState == 103 ?
                    '网关空间不足' : row.upgradeConnState == 104 ?
                    '开始下载' : row.upgradeConnState == 105 ?
                    '下载成功' : row.upgradeConnState == 106 ?
                    '下载失败' : row.upgradeConnState == 107 ?
                    '数据校验正确' : row.upgradeConnState == 108 ?
                    '数据校验错误' : row.upgradeConnState == 109 ?
                    '更新配置成功' : row.upgradeConnState == 110 ?
                    '更新配置失败' : row.upgradeConnState == 111 ?
                    '正在更新固件' : row.upgradeConnState == 112 ?
                    '正在更新配置' : '未知';

            },
            upgradeLowerState(row, column) {
                return row.upgradeLowerState ==-1 ? ' ' : row.upgradeLowerState == 0 ? '不需要更新固件' : row.upgradeLowerState == 1 ? '数据解析错误' : row
                    .upgradeLowerState ==
                    2 ? '读取内存失败' : row.upgradeLowerState == 3 ? '不需要下载' :
                    row.upgradeLowerState == 4 ? '连接服务器失败' : row.upgradeLowerState == 5 ? '连接服务器成功' : row
                    .upgradeLowerState ==
                    6 ? '网关空间不足' : row.upgradeLowerState == 7 ? '内存空间不足' :
                    row.upgradeLowerState == 8 ? '开始下载' : row.upgradeLowerState == 9 ? '下载成功' : row
                    .upgradeLowerState ==
                    10 ? '下载失败' : row.upgradeLowerState == 11 ?
                    '数据校验正确' : row.upgradeLowerState == 12 ? '数据校验错误' : row.upgradeLowerState == 13 ?
                    '内存擦除失败' : row.upgradeLowerState == 14 ? '开始写入内存' :
                    row.upgradeLowerState == 15 ? '写入内存成功' : row.upgradeLowerState == 16 ?
                    '写入内存失败' : row.upgradeLowerState == 17 ? '开始更新固件' :
                    row.upgradeLowerState == 18 ? '更新固件成功' :
                    row.upgradeLowerState == 19 ? '更新固件失败' : row.upgradeLowerState == 20 ? '更新配置成功' : row
                    .upgradeLowerState ==
                    21 ? '更新配置失败' : row.upgradeLowerState == 22 ? '正在更新固件' : row.upgradeLowerState == 23 ?
                    '正在更新配置' : row.upgradeLowerState == 100 ?
                    '数据解析错误' : row.upgradeLowerState == 101 ?
                    '连接服务器失败' : row.upgradeLowerState == 102 ?
                    '连接服务器成功' : row.upgradeLowerState == 103 ?
                    '网关空间不足' : row.upgradeLowerState == 104 ?
                    '开始下载' : row.upgradeLowerState == 105 ?
                    '下载成功' : row.upgradeLowerState == 106 ?
                    '下载失败' : row.upgradeLowerState == 107 ?
                    '数据校验正确' : row.upgradeLowerState == 108 ?
                    '数据校验错误' : row.upgradeLowerState == 109 ?
                    '更新配置成功' : row.upgradeLowerState == 110 ?
                    '更新配置失败' : row.upgradeLowerState == 111 ?
                    '正在更新固件' : row.upgradeLowerState == 112 ?
                    '正在更新配置' : '未知';
            },
            // upgradeConnState(row, column) {
            //     return row.upgradeConnState ==-1 ? ' ' : row.upgradeConnState == 0 ? '不需要升级自身' : row.upgradeConnState == 1 ? '路径解析错误' : row
            //         .upgradeConnState ==
            //         2 ? '读取flash失败' : row.upgradeConnState == 3 ? '不需要下载自身固件' :
            //         row.upgradeConnState == 4 ? '连接ftp服务器失败' : row.upgradeConnState == 5 ? '连接ftp服务器成功' : row
            //         .upgradeConnState ==
            //         6 ? 'ram空间不足' : row.upgradeConnState == 7 ? 'flash空间不足' :
            //         row.upgradeConnState == 8 ? '开始下载自身固件' : row.upgradeConnState == 9 ? '下载自身固件成功' : row
            //         .upgradeConnState ==
            //         10 ? '下载自身固件失败' : row.upgradeConnState == 11 ?
            //         '自身固件CRC校验正确' : row.upgradeConnState == 12 ? '自身固件CRC校验错误' : row.upgradeConnState == 13 ?
            //         'flash存储芯片擦除失败' : row.upgradeConnState == 14 ? '自身固件开始写入flash存储芯片' :
            //         row.upgradeConnState == 15 ? '自身固件写入flash存储芯片成功' : row.upgradeConnState == 16 ?
            //         '自身固件写入flash存储芯片失败' :
            //         row.upgradeConnState == 17 ? '开始升级自身' : row.upgradeConnState == 18 ? '升级自身成功' :
            //         row.upgradeConnState == 19 ? '升级自身失败' : row.upgradeConnState == 20 ? '导入参数成功' : row
            //         .upgradeConnState ==
            //         21 ? '导入参数失败' : row.upgradeConnState == 22 ? '指令执行失败，正在升级' : row.upgradeConnState == 23 ?
            //         '指令执行失败，正在导入参数' : row.upgradeConnState == 100 ?
            //         ' 路径解析错误' : row.upgradeConnState == 101 ?
            //         '连接ftp服务器失败' : row.upgradeConnState == 102 ?
            //         '连接ftp服务器成功' : row.upgradeConnState == 103 ?
            //         'ram空间不足' : row.upgradeConnState == 104 ?
            //         '开始下载参数表' : row.upgradeConnState == 105 ?
            //         '下载参数表成功' : row.upgradeConnState == 106 ?
            //         '下载参数表失败' : row.upgradeConnState == 107 ?
            //         '参数表CRC校验正确' : row.upgradeConnState == 108 ?
            //         '参数表CRC校验错误' : row.upgradeConnState == 109 ?
            //         '导入参数成功' : row.upgradeConnState == 110 ?
            //         '导入参数失败' : row.upgradeConnState == 111 ?
            //         '指令执行失败，正在升级' : row.upgradeConnState == 112 ?
            //         '指令执行失败，正在导入参数' : '未知';

            // },
            // upgradeLowerState(row, column) {
            //     return row.upgradeLowerState ==-1 ? ' ' : row.upgradeLowerState == 0 ? '不需要升级设备' : row.upgradeLowerState == 1 ? '路径解析错误' : row
            //         .upgradeLowerState ==
            //         2 ? '读取flash失败' : row.upgradeLowerState == 3 ? '不需要下载自身固件' :
            //         row.upgradeLowerState == 4 ? '连接ftp服务器失败' : row.upgradeLowerState == 5 ? '连接ftp服务器成功' : row
            //         .upgradeLowerState ==
            //         6 ? 'ram空间不足' : row.upgradeLowerState == 7 ? 'flash空间不足' :
            //         row.upgradeLowerState == 8 ? '开始下载设备固件' : row.upgradeLowerState == 9 ? '下载设备固件成功' : row
            //         .upgradeLowerState ==
            //         10 ? '下载设备固件失败' : row.upgradeLowerState == 11 ?
            //         '设备固件CRC校验正确' : row.upgradeLowerState == 12 ? '设备固件CRC校验错误' : row.upgradeLowerState == 13 ?
            //         'flash存储芯片擦除失败' : row.upgradeLowerState == 14 ? '设备固件开始写入flash存储芯片' :
            //         row.upgradeLowerState == 15 ? '设备固件写入flash存储芯片成功' : row.upgradeLowerState == 16 ?
            //         '设备固件写入flash存储芯片失败' : row.upgradeLowerState == 17 ? '开始升级设备' :
            //         row.upgradeLowerState == 18 ? '升级设备成功' :
            //         row.upgradeLowerState == 19 ? '升级设备失败' : row.upgradeLowerState == 20 ? '导入参数成功' : row
            //         .upgradeLowerState ==
            //         21 ? '导入参数失败' : row.upgradeLowerState == 22 ? '指令执行失败，正在升级' : row.upgradeLowerState == 23 ?
            //         '指令执行失败，正在导入参数' : row.upgradeLowerState == 100 ?
            //         ' 路径解析错误' : row.upgradeLowerState == 101 ?
            //         '连接ftp服务器失败' : row.upgradeLowerState == 102 ?
            //         '连接ftp服务器成功' : row.upgradeLowerState == 103 ?
            //         'ram空间不足' : row.upgradeLowerState == 104 ?
            //         '开始下载参数表' : row.upgradeLowerState == 105 ?
            //         '下载参数表成功' : row.upgradeLowerState == 106 ?
            //         '下载参数表失败' : row.upgradeLowerState == 107 ?
            //         '参数表CRC校验正确' : row.upgradeLowerState == 108 ?
            //         '参数表CRC校验错误' : row.upgradeLowerState == 109 ?
            //         '导入参数成功' : row.upgradeLowerState == 110 ?
            //         '导入参数失败' : row.upgradeLowerState == 111 ?
            //         '指令执行失败，正在升级' : row.upgradeLowerState == 112 ?
            //         '指令执行失败，正在导入参数' : '未知';
            // },
            formatstate(row, column) {

                return row.state == 1 ? '在线' : row.state == 0 ? '离线' : '未知';
            },
            special(index, row) {
                sessionStorage.setItem('facility_details', row.deviceId)
                this.$router.push({
                    path: '/devices/gatewaydis'
                })
            },
            getdata(item, row) {
                this.$confirm('确认升级吗？', '提示', {}).then(() => {
                    let para = {
                        code: item,
                        deviceIdList: [row.deviceId],
                        attrs: {

                        }
                    };
                    this.loading = true;
                    //NProgress.start();
                    device_cmd(para).then((res) => {
                        this.begCode(res.code, res.message);
                        this.$message({
                            message: '已发送命令',
                            type: 'success'
                        });
                        //NProgress.done();
                    });
                });
            },
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getUsers();
            },
            //获取菜单列表
            getUsers() {
                // if(this.menubt.indexOf('device_upgrade/query')==-1){
                //     this.$message({
                //                 message: '没有查询设备升级的权限',
                //                 type: 'warning'
                //             });
                //     return false;
                // }
                let para = {
                    pageNo: this.page,
                    pageSize: this.pageSize,
                    name: this.filters.name,
                    deviceSn: this.filters.deviceSn,
                    deviceTemplateId: this.filters.formula
                };
                // this.listLoading = true;
                //NProgress.start();
                device_attr_query(para).then((res) => {
                     this.begCode(res.code,res.message);
                    this.total = res.data.totalCount;
                    this.query = res.data.list;
                    this.page=res.data.pageNo;
                    // this.listLoading = false;
                    //NProgress.done();
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该选项吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        idList: [row.id]
                    };
                    device_model_delete(para).then((res) => {
                         this.begCode(res.code,res.message);
                        if (res.code == 0) {

                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getUsers();

                        }
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (row, type) {
                console.log(row)
                this.getlist(type);
                this.paramsCode = row.paramsCode
                this.paramsVersion = row.paramsVersion
                this.softCode = row.softCode
                this.softVersion = row.softVersion
                if (type == 2) {
                    this.lowerType = 1;
                } else {
                    this.lowerType = '';
                }
                this.parameters = '0';
                this.editFormVisible = true;
                this.parametype = type;
                this.editForm = {
                    type: type,
                    idList: [row.deviceId],
                    ftpPort: '',
                    ftpUser: '',
                    deviceUpgradeId: '',
                    deviceArgumentId: '',
                    ftpPassword: ''
                }
                let para = {
                    deviceId:row.deviceId
                    }
                    device_slave_list(para).then((res) => {
                        console.log(res)
                        this.sigleslavelist = res.data.list
                    })
                    this.dellist = [row.deviceId]
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    remark: ''
                };
            },
            //编辑
            editSubmit: function () {
                if (!this.editForm.deviceArgumentId && !this.editForm.deviceUpgradeId) {
                    this.$message({
                        message: '请选择固件包或参数表',
                        type: 'warning'
                    });
                    return false;
                }
                if ( !this.editForm.deviceArgumentId) {
                    this.$message({
                        message: '请选择参数表',
                        type: 'warning'
                    });
                    return false;
                }
                if(!this.editForm.slaveSN){
                    this.$message({
                        message: '请填写地址号',
                        type: 'warning'
                    });
                    return false; 
                }
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            // this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);

                            device_upgrade_batch_upgrade(para).then((res) => {
                                this.begCode(res.code, res.message);
                                console.log(res)
                                if (res.code == 0) {
                                    this.editLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);

                            device_model_post(para).then((res) => {
                                 this.begCode(res.code,res.message);
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getUsers();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            getlist(type) {
                console.log(name)
                this.bag = [];
                this.parameter = [];
                let para = {
                    pageNo: 1,
                    pageSize: 9999,
                    type: type
                }
                // let para1
                // if(name == '远程设备升级'){
                //     para1 = {
                //         pageNo: 1,
                //         pageSize: 9999,
                //         type: type,
                //         version: this.dlSoftVersion
                //     }
                // }else{
                //     para1 = {
                //         pageNo: 1,
                //         pageSize: 9999,
                //         type: type,
                //     }
                // }
                // console.log(para1)
                device_upgrade_query(para).then((res) => {
                     this.begCode(res.code,res.message);
                    this.listLoading = false;
                    for (let i in res.data.list) {
                        if (res.data.list[i].state == 1) {
                            this.bag.push(res.data.list[i])
                        }
                    }
                });
                device_argument_query(para).then((res) => {
                     this.begCode(res.code,res.message);
                    this.listLoading = false;
                    for (let i in res.data.list) {
                        if (res.data.list[i].state == 1) {
                            this.parameter.push(res.data.list[i])
                        }
                    }
                    // this.parameter = res.data.list;
                });
            },
            //批量删除
            batchRemove: function (type) {


                this.parameters = '0';
                var ids = this.sels.map(item => item.deviceId).toString();
                this.getlist(type)
                this.editFormVisible = true;
                this.editForm = {
                    type: type,
                    idList: ids.split(','),
                    ftpUser: '',
                    ftpPort: '',
                    deviceUpgradeId: '',
                    deviceArgumentId: '',
                    ftpPassword: ''
                }
                //NProgress.start();

                // device_model_delete(para).then((res) => {
                //      this.begCode(res.code,res.message);
                //     this.listLoading = false;
                //     //NProgress.done();
                //     this.$message({
                //         message: '删除成功',
                //         type: 'success'
                //     });
                //     this.getUsers();
                // });

            },
            batch(){
                this.dellist = [];
                var ids = this.sels.map(item => item.deviceId).toString();
                this.dellist = ids.split(",");
            },
            // 远程设备下载/升级（批量）
            downloadEdit:function (type,name){
                if(name == "远程固件下载"){
                    this.downloadVisible = true;
                }else if(name == "远程固件升级"){
                     this.farUpgradeVisible = true
                }
                
                let obj = {}
                obj = this.formulalist.find((item) => {
                    return  item.id === this.filters.formula 
                })
                this.slavelist = JSON.parse(obj.deviceSlaveList)
                
                this.getlist(type);
                // console.log(this.bag)
                console.log(this.slavelist)
                this.batch()
                // console.log(this.dellist)
                this.firmwareForm = {
                    deviceUpgradeId: '',
                    slaveSn: ''
                }
            },
            //远程设备下载/升级(单个)
            farUpgradeEdit:function(type,row,name){
                console.log(row)
                 this.dlSoftCode = ''
                 this.dlSoftVersion = ''
                // if(row != ''){
                //     this.dlSoftCode = row.dlSoftCode
                //     this.dlSoftVersion = row.dlSoftVersion
                // }
                if(name == "远程设备下载"){
                    this.sigledownloadVisible = true;
                }else if(name == "远程设备升级"){
                     this.siglefarUpgradeVisible = true
                }
               
                if(row != ''){//如果有row就是单个的没有就是群控
                    let para = {
                    deviceId:row.deviceId
                    }
                    device_slave_list(para).then((res) => {
                        console.log(res)
                        this.sigleslavelist = res.data.list
                    })
                    this.dellist = [row.deviceId]
                    console.log(row)
                    this.firmwareForm.slaveSn = row.deviceSlaveList[0].slaveSn
                }else{
                    let obj = {}
                    obj = this.formulalist.find((item) => {
                        return  item.id === this.filters.formula 
                    })
                    this.sigleslavelist = JSON.parse(obj.deviceSlaveList)
                    // this.firmwareForm.slaveSn = sigleslavelist[0].slaveSn
                    this.batch()
                }
                

               this.getlist(2);
               this.bagslave = this.bag
                
                // this.firmwareForm = {
                //     deviceUpgradeId: '',
                //     slaveSn: ''
                // }
            },
            selectSlave(){  
                let obj = {}
                obj = this.sigleslavelist.find((item) => { 
                    return item.slaveSn === this.firmwareForm.slaveSn
                })
                console.log(obj)
                this.dlSoftCode = obj.dlSoftCode
                this.dlSoftVersion = obj.dlSoftVersion
                let para = {
                    pageNo: 1,
                    pageSize: 9999,
                    type: 2,
                    version: this.dlSoftVersion
                }
                console.log(para)
                device_upgrade_query(para).then((res) => {
                console.log(res)
                    this.begCode(res.code,res.message);
                    this.listLoading = false;
                    this.bagslave = res.data.list
                });
            },
            //下载
            download:function(){
                if (!this.firmwareForm.deviceUpgradeId) {
                        this.$message({
                            message: '请选择固件包',
                            type: 'warning'
                        });
                        return false;
                    }
                    // if (!this.firmwareForm.slaveSn) {
                    //     this.$message({
                    //         message: '请选择从机',
                    //         type: 'warning'
                    //     });
                    //     return false;
                    // }
                let para = {
                    type: 2,
                    deviceUpgradeId: this.firmwareForm.deviceUpgradeId,
                    idList: this.dellist,
                    slaveSN: this.firmwareForm.slaveSn
                }
                // console.log(para)
                device_batch_download(para).then((res) => {
                    this.begCode(res.code, res.message);
                    console.log(res)
                    if (res.code == 0){
                        //NProgress.done();
                        this.$message({
                            message: '提交成功',
                            type: 'success'
                        });
                        this.downloadVisible = false;
                        this.sigledownloadVisible = false
                        this.getUsers();
                    }
                })
            },
            //远程升级
            farUpgrade:function(){
                if (!this.firmwareForm.deviceUpgradeId) {
                        this.$message({
                            message: '请选择固件包',
                            type: 'warning'
                        });
                        return false;
                    }
                    // if (!this.firmwareForm.slaveSn) {
                    //     this.$message({
                    //         message: '请选择从机',
                    //         type: 'warning'
                    //     });
                    //     return false;
                    // }
                let para = {
                    type: 2,
                    deviceUpgradeId: this.firmwareForm.deviceUpgradeId,
                    idList: this.dellist,
                    slaveSN: this.firmwareForm.slaveSn
                }
                console.log(para)
                device_batch_upgrade(para).then((res) => {
                    this.begCode(res.code, res.message);
                    console.log(res)
                    if (res.code == 0){
                        //NProgress.done();
                        this.$message({
                            message: '提交成功',
                            type: 'success'
                        });
                        this.farUpgradeVisible = false;
                        this.siglefarUpgradeVisible = false
                        this.getUsers();
                    }
                })
            },
            getFarMessage(){     
                this.$confirm('确认获取远程固件信息吗？', '提示', {}).then(() => {
                    this.batch()
                    
                    let para = {
                            code: 'getOTAVersion',
                            deviceIdList:this.dellist,
                        };
                        device_cmd(para).then((res) => {
                            this.begCode(res.code, res.message);
                            console.log(res)
                            this.$message({
                                message: '已发送命令',
                                type: 'success'
                            });
                        });
                });
            },
        },
        mounted() {

            this.menubt = localStorage.getItem('menubt');
          
            this.getUsers();
            this.t = setInterval(() => {
                let para = {
                    pageNo: this.page,
                    pageSize: 10,
                    name: this.filters.name,
                    deviceSn: this.filters.deviceSn,
                    deviceTemplateId: this.filters.formula
                };
                // this.listLoading = true;
                //NProgress.start();
                device_attr_query(para).then((res) => {
                     this.begCode(res.code,res.message);
                    for (let i in res.data.list) {

                        this.query[i].deviceId = res.data.list[i].deviceId;
                        this.query[i].deviceName = res.data.list[i].deviceName;
                        this.query[i].deviceSn = res.data.list[i].deviceSn;
                        this.query[i].lastUpgradeConnTime = res.data.list[i].lastUpgradeConnTime;
                        this.query[i].upgradeLowerState = res.data.list[i].upgradeLowerState;
                        this.query[i].upgradeConnState = res.data.list[i].upgradeConnState;
                        console.log(res.data.list[i].upgradeLowerState)
                        
                        this.query[i].upgradeLowerProgress = res.data.list[i].upgradeLowerProgress;
                      
                        this.query[i].lastUpgradeLowerTime = res.data.list[i].lastUpgradeLowerTime;
                        // this.query[i].upgradeLowerProgress = res.data.list[i].upgradeLowerProgress;
                        this.query[i].state = res.data.list[i].state;

                    }
                    console.log(this.query)
                    // this.query = res.data.list;
                    // this.listLoading = false;
                    //NProgress.done();
                });
 

            }, 10000)
            let para2 = {
                    // pageNo: this.page,
                    pageSize:9999,
                };
            device_template_query(para2).then((res) => {
                    this.formulalist = res.data.list
                    console.log(this.formulalist)
                })
        },
        destroyed: function () {
            clearInterval(this.t)
        },
    }
</script>

<style>
    .el-progress-bar__innerText {
        color: #000
    }
</style>